package com.jumaojiang.controller;

import com.jumaojiang.exceptions.ResourceException;
import com.jumaojiang.pojo.ResourceGetTime;
import com.jumaojiang.pojo.RsFile;
import com.jumaojiang.pojo.RsVideo;
import com.jumaojiang.service.ResourceService;
import com.jumaojiang.utils.JWTUtil;
import com.jumaojiang.vo.AjaxResultVo;
import com.jumaojiang.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * javaee-rs
 *
 * @author wuhanwei
 * @version 1.0
 * @date 2021/11/8
 */
@Controller
@ResponseBody
@RequestMapping("resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    // 获取目录资源列表
    @RequestMapping(value = "list/{user-id}", method = RequestMethod.GET)
    public AjaxResultVo queryList(HttpServletRequest request, @PathVariable("user-id") Integer userId){
        Map<Integer, QueryVo> map = resourceService.queryList(userId);
        if(map != null){
            // 将cookie通过ajax响应传递到前端,让前端设置cookie
            Object cookie = request.getAttribute("cookie");
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("resource", map);
            paramMap.put("cookie",cookie.toString());
            return new AjaxResultVo(paramMap);
        }
        return new AjaxResultVo(500, "服务器内部异常", null);
    }

    // 获取用户查询次数
    @RequestMapping(value = "get-time/{user-id}", method = RequestMethod.GET)
    public AjaxResultVo queryGetTime(@PathVariable("user-id") Integer userId){
        ResourceGetTime resourceGetTime = resourceService.queryGetTime(userId);
        if(resourceGetTime != null){
            return new AjaxResultVo(resourceGetTime);
        }
        return new AjaxResultVo(500, "服务器内部异常", null);
    }

    // 获取一次file
    @RequestMapping(value = "file-get/{file-id}", method = RequestMethod.GET)
    public AjaxResultVo fileGet(@PathVariable("file-id") Integer fileId,
                                @RequestParam(value = "isOwn", required = false) String isOwn,
                                HttpServletRequest request) throws ResourceException {
        // 查询次数减一更新 & 添加资源
        boolean b = resourceService.fileGet(request, fileId, isOwn);
        if( !b ){
            return new AjaxResultVo(500, "服务器内部异常", null);
        }
        // 查询资源
        RsFile rsFile = resourceService.queryFileById(fileId);
        if(rsFile == null){
            return new AjaxResultVo(500, "服务器内部异常", null);
        }
        // 查询用户查询次数
        Integer userId = JWTUtil.parseUserId(request);
        ResourceGetTime resourceGetTime = resourceService.queryGetTime(userId);
        // 封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("rsFile", rsFile);
        map.put("resourceGetTime", resourceGetTime);
        return new AjaxResultVo(map);
    }

    // 获取一次video
    @RequestMapping(value = "video-get/{video-id}", method = RequestMethod.GET)
    public AjaxResultVo videoGet(@PathVariable("video-id") Integer videoId,
                                 @RequestParam(value = "isOwn", required = false) String isOwn,
                                HttpServletRequest request) throws ResourceException {
        // 查询次数减一更新 & 添加资源
        boolean b = resourceService.videoGet(request, videoId, isOwn);
        if( !b ){
            return new AjaxResultVo(500, "服务器内部异常", null);
        }
        // 查询资源
        RsVideo rsVideo = resourceService.queryVideoById(videoId);
        if(rsVideo == null){
            return new AjaxResultVo(500, "服务器内部异常", null);
        }
        // 查询用户查询次数
        Integer userId = JWTUtil.parseUserId(request);
        ResourceGetTime resourceGetTime = resourceService.queryGetTime(userId);
        // 封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("rsVideo", rsVideo);
        map.put("resourceGetTime", resourceGetTime);
        return new AjaxResultVo(map);
    }


    // 添加视频信息
    @RequestMapping(value = "video", method = RequestMethod.POST)
    public AjaxResultVo addVideo(HttpServletRequest request, RsVideo rsVideo) throws ResourceException {
        Integer integer = resourceService.videoAdd(request, rsVideo);
        if(integer > 0){
            return new AjaxResultVo();
        }
        return new AjaxResultVo(500, "服务器内部异常", null);
    }


    // 添加文件信息
    @RequestMapping(value = "file", method = RequestMethod.POST)
    public AjaxResultVo addFile(HttpServletRequest request, RsFile rsFile) throws ResourceException {
        Integer integer = resourceService.fileAdd(request, rsFile);
        if(integer > 0){
            return new AjaxResultVo();
        }
        return new AjaxResultVo(500, "服务器内部异常", null);
    }
}















